package leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 描述:222. 完全二叉树的节点个数
 *
 * @author Madison You
 * @created 17:44
 */
public class Mid_222_CountNodes {
    public static void main(String[] args) {

    }

    public int countNodes(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) return 0;
        queue.offer(root);
        int sum = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            sum += size;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
        }
        return sum;
    }
}
